<?php
/*
 +----------------------------------------------------------------------
 + Title        : Base 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/6/9 18:00
 + Last-time    : 2019/6/9 18:00+ 86187
 + Desc         : Base
 +----------------------------------------------------------------------
*/


namespace app\common\service;


use app\common\lib\Where;
use think\Model;

class BaseService extends Model
{
    //获取门店数据
    public function getAll(Where $where)
    {
        $result =  \model($this->table)
            ->where($where->getWhere())
            ->order($where->getOrder())
            ->paginate($where->getLimit());
        return $result;
    }

    public function getOne($where = 1)
    {
        //默认获取主键为1的数据
        return model($this->table)->get($where);
    }

    //查找并更新
    public function getAndEdit($where = null, $data = null)
    {
        if (empty($data) || empty($where)) {
            return '$data或$where变量为空';
        }
        //默认获取主键为1的数据
        return model($this->table)
            ->get($where)
            ->save($data);
    }

    //主键更新
    public function updateid($data)
    {
        return model($this->table)->update($data);
    }

    //条件更新
    public function updateWhere($data, $where)
    {
        return model($this->table)->where($where)->update($data);
    }

    //批量更新
    public function allSave($data)
    {
        //$data必须为二维数组
        return model($this->table)->saveAll($data);
    }



    public function findInfo($where = [], $order = '')
    {
        return \model($this->table)
            ->where($where)
            ->order($order)
            ->find();
    }

    public function add($data)
    {
        $res = model($this->table)
            ->allowField(true)
            ->create($data , true);
        return $res;
    }



    public function addAll($data)
    {
        return \model($this->table)->saveAll($data);
    }


    public function del($where)
    {
        return model($this->table)
            ->destroy($where);
    }

    public function  whereParse(Where $where ){
        return $this->where($where->getWhere());
    }
}